﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ZG_member
{
    public partial class MembercartTypeChart : Form
    {
        public MembercartTypeChart()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;

        private void MembercartTypeChart_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
        public void sousuo(string tiaojian)
        {
            BLL.memberInfo m = new BLL.memberInfo();
            DataSet ds;
            if (tiaojian == "")
            {
                ds = m.GetListSql("select c.solutionName as 会员等级, count(cardtype) as 数量 from dbo.memberInfo m inner join dbo.consumeSolution c on(c.id = m.cardType) group by  cardtype, c.solutionName");

            }
            else
            {
                ds = m.GetListSql("select c.solutionName as 会员等级, count(cardtype) as 数量 from dbo.memberInfo m inner join dbo.consumeSolution c on(c.id = m.cardType) where "+tiaojian+" group by  cardtype, c.solutionName");

            }
            decimal zonghe = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString());
            }
            List<string> name = new List<string>();
            List<string> bili = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                string aa = ((100 / zonghe) * decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString())).ToString();

                bili.Add(aa);
                name.Add(ds.Tables[0].Rows[i]["会员等级"].ToString() + " 比例：" + decimal.Parse(aa).ToString("F1") + "%");

            }
            chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
            chart1.Series["Series1"]["PieLinelColor"] = "Red";
            chart1.Series[0].Points.DataBindXY(name, bili);
            chart1.DataBind();






            //DataSet ds;
            //if (tiaojian == "")
            //{
            //    ds = m.GetListSql("select month(birthDay) as 月, count(month(birthDay)) as 数量 from dbo.memberInfo group by month(birthDay)");

            //}
            //else
            //{
            //    ds = m.GetListSql("select month(birthDay) as 月, count(month(birthDay)) as 数量 from dbo.memberInfo where " + tiaojian + " group by month(birthDay)");

            //}




            //decimal zonghe = 0;
            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{
            //    zonghe = zonghe + decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString());
            //}
            //List<string> name = new List<string>();
            //List<string> bili = new List<string>();
            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{


            //    string aa = ((100 / zonghe) * decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString())).ToString();

            //    bili.Add(aa);
            //    name.Add(ds.Tables[0].Rows[i]["月"].ToString() + "月份 比例：" + decimal.Parse(aa).ToString("F1") + "%");

            //}
            //chart2.Series["Series1"]["PieLabelStyle"] = "Outside";
            //chart2.Series["Series1"]["PieLinelColor"] = "Red";
            //chart2.Series[0].Points.DataBindXY(name, bili);
            //chart2.DataBind();

        }
        private void MembercartTypeChart_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            BLL.Areas ars = new BLL.Areas();
            this.cbojiguan.DisplayMember = "AreaName";
            this.cbojiguan.ValueMember = "id";
            this.cbojiguan.DataSource = ars.GetList("").Tables[0];


            BLL.consumeSolution cm = new BLL.consumeSolution();
            this.cbohuiyuandengji.DataSource = cm.GetModelList("");
            this.cbohuiyuandengji.DisplayMember = "solutionName";
            this.cbohuiyuandengji.ValueMember = "id";

            this.cbohuiyuandengji.SelectedIndex = 0;
            this.cboSex.SelectedIndex = 0;
            this.cbojiguan.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (this.cboSex.Text != "全部")
            {
                sql += " sex = ";
                sql += this.cboSex.Text == "男" ? 1 : 0;
                sql += " and ";
            }
            sql += "cardType = ";
            sql += this.cbohuiyuandengji.SelectedValue;
            sql += " and  AreasID =  ";
            sql += this.cbojiguan.SelectedValue;
            sql += "  ";



            sousuo(sql);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sousuo("");
        }

        
    }
}
